﻿/************************************************************************

  Extended Silverlight Toolkit – SAMPLE CODE

  Copyright (C) 2010-2012 Xceed Software Inc.

  This program is provided to you under the terms of the Microsoft Public
  License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

  Please purchase a commercial version of this toolkit if you are using
  it in a commercial product. The commercial versions support the project,
  and also include more features for each control, priority support,
  source code, updates, removed watermark, and a proprietary license.

  Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Xceed.Utils.Silverlight.ListBox.Samples
{
  public class RatingConverter : IValueConverter
  {
    public RatingConverter()
    {
      this.Maximum = 4; //default value of 5 for Maximum
    }

    public DataTemplate FullTemplate
    {
      get;
      set;
    }

    public DataTemplate HalfTemplate
    {
      get;
      set;
    }

    public DataTemplate EmptyTemplate
    {
      get;
      set;
    }

    public int Minimum
    {
      get;
      set;
    }

    public int Maximum
    {
      get;
      set;
    }


    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
      List<UIElement> images = new List<UIElement>();

      double realValue = System.Convert.ToDouble( value );

      for( int i = this.Minimum; i <= this.Maximum; i++ )
      {
        ContentPresenter contentPresenter = new ContentPresenter();

        if( ( realValue - i ) >= 1d )
        {
          contentPresenter.ContentTemplate = this.FullTemplate;
        }
        else if( ( realValue - i ) >= 0.5d )
        {
          contentPresenter.ContentTemplate = this.HalfTemplate;
        }
        else
        {
          contentPresenter.ContentTemplate = this.EmptyTemplate;
        }

        images.Add( contentPresenter );
      }

      return images;
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
      return ( ( ObservableCollection<Image> )value ).Count;
    }
  }
}
